<?php
/**
 * Created by PhpStorm.
 * User: mr_lee
 * Date: 2017/10/16
 * Time: 下午4:13
 */

namespace app\agenttest\controller;

use app\common\model\MchStatis;
use app\common\model\Merchant;
use app\common\model\Mstatis;
use app\common\model\Paylog;
use app\common\model\Tklog;
use think\Request;
use think\Session;


class Pay extends AgenttestBase
{
    //交易统计
    public function paycount()
    {

        if (Request()->isAjax()) {
            $param = request()->post();
            if ($param['type'] == 'search') {

                $Paylog = new Paylog();
                $Tklog = new Tklog();
                if ($param['search_date'] !== '') {
                    $searchdate = explode(" - ", request()->param('search_date'));
                    $starttime = strtotime($searchdate[0]);
                    $endtime = strtotime($searchdate[1]);
                } else {
                    $starttime = strtotime(date('Y-m-d'));
                    $endtime = time();
                }
                $map['add_time'] = [['EGT', $starttime], ['ELT', $endtime], 'and'];
                $map['pay_cate'] = array('neq', "recharge");
                if (isset($param['mchid']) && !empty($param['mchid'])) {

                    $mids = $param['mchid'];
                } else {
                    $mids = $this->get_mer_mid();
                }
                $map['mid'] = ['in', $mids];


                $maptk['addtime'] = array(array('egt', $starttime), array('elt', $endtime), "and");
                $maptk['mid'] = $map['mid'];
                $map['pay_statu'] = array(array('neq', 0), array('neq', 8), 'and');
                $map['pay_cate'] = array('neq', 'recharge');
                //微信支付
                $map1 = $map;
                $map1['pay_fs'] = array('in', [0, 2]);
                $arrwx = $Paylog->where($map1)->field('count(id) as wx_order,sum(price) as wx_fee,sum(yf_price) as wx_yf_fee,sum(zk_price) as wx_zk_fee,sum(service_charge) as wx_service_charge')->find();

                $arr['wx_order'] = $arrwx['wx_order'];                //微信笔数
                $arr['wx_fee'] = !empty($arrwx['wx_fee']) ? $arrwx['wx_fee'] : "0.00";         //微信金额
                $arr['wx_service_charge'] = !empty($arrwx['wx_service_charge']) ? $arrwx['wx_service_charge'] : "0.00";         //微信手续费

                //支付宝支付
                $map3 = $map;
                $map3['pay_fs'] = array('in', [1, 3]);
                $arrali = $Paylog->where($map3)->field('count(id) as ali_order,sum(price) as ali_fee,sum(yf_price) as ali_yf_fee,sum(zk_price) as ali_zk_fee,sum(service_charge) as ali_service_charge')->find();

                $arr['ali_order'] = $arrali['ali_order'];               //支付宝笔数
                $arr['ali_fee'] = !empty($arrali['ali_fee']) ? $arrali['ali_fee'] : "0.00";           //支付宝金额
                $arr['ali_service_charge'] = !empty($arrali['ali_service_charge']) ? $arrali['ali_service_charge'] : "0.00";         //支付宝手续费

                //POS支付
                $map3 = $map;
                $map3['pay_fs'] = array('in', [5]);
                $arrpos = $Paylog->where($map3)->field('count(id) as pos_order,sum(price) as pos_fee,sum(yf_price) as pos_yf_fee,sum(zk_price) as pos_zk_fee,sum(service_charge) as pos_service_charge')->find();

                $arr['pos_order'] = $arrpos['pos_order'];               //支付宝笔数
                $arr['pos_fee'] = !empty($arrpos['pos_fee']) ? $arrpos['pos_fee'] : "0.00";           //支付宝金额
                $arr['pos_service_charge'] = !empty($arrpos['pos_service_charge']) ? $arrpos['pos_service_charge'] : "0.00";         //POS手续费

                //会员卡支付
                $map5 = $map;
                $map5['pay_fs'] = 8;
                $arrwxcard = $Paylog->where($map5)->field('count(id) as wx_card_order,sum(price) as wx_card_fee,sum(yf_price) as wx_card_yf_fee,sum(zk_price) as wx_card_zk_fee')->find();

                $arr['wx_card_order'] = $arrwxcard['wx_card_order'];                       //会员卡笔数
                $arr['wx_card_fee'] = !empty($arrwxcard['wx_card_fee']) ? $arrwxcard['wx_card_fee'] : "0.00";         //会员卡金额

                //现金支付
                $map6 = $map;
                $map6['pay_fs'] = 4;
                $arrcash = $Paylog->where($map6)->field('count(id) as cash_order,sum(price) as cash_fee,sum(yf_price) as cash_yf_fee,sum(zk_price) as cash_zk_fee')->find();

                $arr['cash_order'] = $arrcash['cash_order'];                      //现金订单数
                $arr['cash_fee'] = !empty($arrcash['cash_fee']) ? $arrcash['cash_fee'] : "0.00";         //现金金额

//          //合计

                $maptk['tk_status'] = 1;
                $arr['refund_order'] = $Tklog->where($maptk)->count();        //总退款数
                $arr['refund_fee'] = $Tklog->where($maptk)->sum('refund_fee');          //总退款
                $map7 = $map;
                $map7['pay_fs'] = array('in', [0, 1, 2, 3, 4, 8]);
                $z_arr = $Paylog->where($map7)->field('sum(price) as real_fee,count(id) as real_order,sum(yf_price) as ought_fee,sum(zk_price) as dis_fee,avg(price) as avg_fee')->find();
                $real_fee = $z_arr['real_fee'] - $arr['refund_fee'];
                $arr['real_fee'] = !empty($real_fee) ? $real_fee : "0.00";
                $arr['real_order'] = !empty($z_arr['real_order']) ? $z_arr['real_order'] : "0.00";
                $arr['dis_fee'] = !empty($z_arr['dis_fee']) ? $z_arr['dis_fee'] : "0.00";                            //总实收金额
                $arr['ought_fee'] = !empty($z_arr['ought_fee']) ? $z_arr['ought_fee'] : "0.00";          //总应付
                $arr['avg_fee'] = !empty($z_arr['avg_fee']) ? $z_arr['avg_fee'] : "0.00";                     //总平均
                $arr['service_charge'] = $arr['wx_service_charge']+$arr['ali_service_charge']+$arr['pos_service_charge'];

                $infos2 = $this->statisinfo($starttime, $endtime, $mids);
                $p_data = request()->post();
                if ($arr) {
                    $data['code'] = 0;
                    $data['message'] = 'success';
                    $data['data']['p_data'] = $p_data;
                    $data['data']['mach'] = $infos2;
                    $data['data']['jy'] = $arr;
                    $data['time'] = $map;
//                $data['sql'] = $sql;
                } else {
                    $data['code'] = 9001;
                    $data['message'] = 'success';
                    $data['data']['p_data'] = $p_data;
                    $data['data']['mach'] = $infos2;
                    $data['data']['jy'] = $arr;
                }
                return json($data);
            } elseif ($param['type'] == "out_excel") {
                if ($param['search_date'] !== '') {
                    $searchdate = explode(" - ", request()->param('search_date'));
                    $starttime = strtotime($searchdate[0]);
                    $endtime = strtotime($searchdate[1]);
                } else {
                    $starttime = strtotime(date('Y-m-d'));
                    $endtime = time();
                }
                $map['add_time'] = [['EGT', $starttime], ['ELT', $endtime], 'and'];
                $map['pay_cate'] = array('neq', "recharge");
                if (isset($param['mchid']) && !empty($param['mchid'])) {

                    $mids = $param['mchid'];
                } else {
                    $mids = $this->get_mer_mid();
                }
                $list = $this->statisinfo($starttime, $endtime, $mids);
                $pub = new \app\common\controller\Pub();
                $header = ['门店编号', '门店名称', '微信收款金额', "微信收款笔数", "微信收款手续费",'支付宝收款金额',
                    '支付宝收款笔数', "支付宝收款手续费",'POS收款金额', "POS收款笔数", "POS收款手续费",
                    '现金收款金额', '现金收款笔数', '微信会员卡支付金额', '微信会员卡支付笔数',
                    '优惠金额', '应收金额', '客单价', '订单数量', '实收金额', '退款金额', '退款笔数']; //表头,名称可自定义

                $excel = $pub->exportExcel($this->getmchname_mch($mids) . chr(10) . "统计时间:" . date('Y-m-d H:i:s', $starttime) . " - " . date('Y-m-d H:i:s', $endtime), $header, collection($list)->toArray(), 'paycount_' . time() . rand(1000, 9999), "门店交易统计", "paycount", false);;

                return "http://" . request()->server('HTTP_HOST') . "/" . $excel;
            }
        } else {
            if ($this->get_mer_mid() !== '') {
                $mapo['id'] = array('in', $this->get_mer_mid());
            } else {
                $mapo['id'] = '';
            }
            $p_data['machname'] = "";
            $today = strtotime(date('Y-m-d'));
            $p_data['search_date'] = date('Y-m-d H:i:s', $today) . " - " . date('Y-m-d H:i:s');
            $machlist = $this->getmchlist();
            $this->assign('p_data', $p_data);
            $this->assign('machlist', json_decode($machlist, true));
            return $this->fetch();
        }

    }

    //门店交易统计封装
    private function statisinfo($starttime, $endtime, $mids)
    {
        $condition = "p.mid = a.id and  pay_cate <> 'recharge' and p.pay_statu <> 0 and p.pay_statu <> 8 and (p.add_time >= :start_time and p.add_time <= :end_time)";
        $param = ['start_time' => $starttime, 'end_time' => $endtime];
        //微信支付统计
        $condition_1 = $condition . "  and p.pay_fs in (0,2)";
        $field = 'a.id as mid,a.merchant as merchant,a.top_mid,a.agent,sum(p.price) as wx_fee,count(p.id) as wx_order,sum(p.service_charge) as wx_service_charge';
        $wxpay = $this->statissql($condition_1, $param, $field, $mids);

        //支付宝支付统计
        $condition_2 = $condition . "  and p.pay_fs in (1,3)";
        $field = 'a.id as mid,a.merchant as merchant,a.top_mid,a.agent,sum(p.price) as ali_fee,count(p.id) as ali_order,sum(p.service_charge) as ali_service_charge';
        $alipay = $this->statissql($condition_2, $param, $field, $mids);

        //POS支付统计
        $condition_2 = $condition . "  and p.pay_fs = 5";
        $field = 'a.id as mid,a.merchant as merchant,a.top_mid,a.agent,sum(p.price) as pos_fee,count(p.id) as pos_order,sum(p.service_charge) as pos_service_charge';
        $pospay = $this->statissql($condition_2, $param, $field, $mids);

        //现金支付统计
        $condition_3 = $condition . "  and p.pay_fs = 4";
        $field = 'a.id as mid,a.merchant as merchant,a.top_mid,a.agent,sum(p.price) as cash_fee,count(p.id) as cash_order';

        $cashpay = $this->statissql($condition_3, $param, $field, $mids);
        //会员卡支付统计
        $condition_4 = $condition . "  and p.pay_fs = 8";
        $field = 'a.id as mid,a.merchant as merchant,a.top_mid,a.agent,sum(p.price) as wx_card_fee,count(p.id) as wx_card_order';
        $wxcardpay = $this->statissql($condition_4, $param, $field, $mids);
        //折扣 应收 实收统计
        $condition_o = $condition . "";
        $field = 'a.id as mid,a.merchant as merchant,sum(p.zk_price) as dis_fee,a.top_mid,a.agent,sum(p.yf_price) as ought_fee,sum(p.price) as real_fee,count(p.id) as real_order,avg(p.price) as avg_fee';
        $othercount = $this->statissql($condition_o, $param, $field, $mids);
        //退款统计
        $Merchant = new Merchant();
        $other2 = $Merchant
            ->alias('a')
            ->join('wn_tklog t', 't.mid = a.id and t.tk_status = 1 and (t.addtime >= :start_time and t.addtime <= :end_time)', 'left')
            ->group('a.id')
            ->where('a.id', 'in', $mids)
            ->bind($param)
            ->field('a.id as mid,a.merchant as merchant,a.top_mid,a.agent,sum(t.refund_fee) as refund_fee,count(t.id) as refund_order')
            ->select();

        $infos = array_merge($wxpay, $alipay, $pospay,$cashpay, $wxcardpay, $othercount, $other2);
        $mchlist = array_unique(array_column($wxpay, 'mid'));
        foreach ($mchlist as $k => $v) {
            foreach ($infos as $ks => $vs) {
                if ($vs['mid'] == $v) {
                    $infos2[$k]['mid'] = $vs['mid'];
                    $infos2[$k]['merchant'] = $vs['merchant'];

                    if (isset($vs['wx_order'])) {
                        $infos2[$k]['wx_fee'] = !empty($vs['wx_fee']) ? $vs['wx_fee'] : 0.00;
                        $infos2[$k]['wx_order'] = $vs['wx_order'];
                        $infos2[$k]['wx_service_charge'] = !empty($vs['wx_service_charge']) ? $vs['wx_service_charge'] : 0.00;
                    }
                    if (isset($vs['real_order'])) {
                        $infos2[$k]['dis_fee'] = !empty($vs['dis_fee']) ? $vs['dis_fee'] : 0.00;
                        $infos2[$k]['ought_fee'] = !empty($vs['ought_fee']) ? $vs['ought_fee'] : 0.00;
                        $infos2[$k]['avg_fee'] = !empty($vs['avg_fee']) ? $vs['avg_fee'] : 0.00;
                        $infos2[$k]['real_fee'] = !empty($vs['real_fee']) ? $vs['real_fee'] : 0.00;
                        $infos2[$k]['real_order'] = !empty($vs['real_order']) ? $vs['real_order'] : 0;
                    }
                    if (isset($vs['ali_order'])) {
                        $infos2[$k]['ali_fee'] = !empty($vs['ali_fee']) ? $vs['ali_fee'] : 0.00;
                        $infos2[$k]['ali_order'] = $vs['ali_order'];
                        $infos2[$k]['ali_service_charge'] = !empty($vs['ali_service_charge']) ? $vs['ali_service_charge'] : 0.00;
                    }
                    if (isset($vs['pos_order'])) {
                        $infos2[$k]['pos_fee'] = !empty($vs['pos_fee']) ? $vs['pos_fee'] : 0.00;
                        $infos2[$k]['pos_order'] = $vs['pos_order'];
                        $infos2[$k]['pos_service_charge'] = !empty($vs['pos_service_charge']) ? $vs['pos_service_charge'] : 0.00;
                    }
                    if (isset($vs['cash_order'])) {
                        $infos2[$k]['cash_fee'] = !empty($vs['cash_fee']) ? $vs['cash_fee'] : 0.00;
                        $infos2[$k]['cash_order'] = $vs['cash_order'];
                    }
                    if (isset($vs['wx_card_order'])) {
                        $infos2[$k]['wx_card_fee'] = !empty($vs['wx_card_fee']) ? $vs['wx_card_fee'] : 0.00;
                        $infos2[$k]['wx_card_order'] = $vs['wx_card_order'];
                    }
                    if (isset($vs['refund_order'])) {

                        $infos2[$k]['refund_fee'] = !empty($vs['refund_fee']) ? $vs['refund_fee'] : 0.00;
                        $infos2[$k]['refund_order'] = $vs['refund_order'];

                    }
                }
            }
        }
        foreach ($infos2 as $k => $v) {
            $infos2[$k]['real_fee'] = $v['real_fee'] - $v['refund_fee'];
        }
        return $infos2;
    }

    private function statissql($condition, $param, $field, $maps)
    {
        $Merchant = new Merchant();
        $map['a.id'] = array('in', $maps);
        $sql = $Merchant
            ->alias('a')
            ->join('wn_paylog p', $condition, "LEFT")
            ->where($map)
            ->group('a.id')
            ->bind($param)
            ->field($field)
            ->select();

//        return $sql;

        return collection($sql)->toArray();

    }


    //交易明细记录
    public function paylog(Request $request)
    {
        if ($request->isAjax()) {
            $param = $request->post();

            if (!empty($param['startprice']) && !empty($param['endprice'])) {
                $map['price'] = array(array('egt', $param['startprice']), array('elt', $param['endprice']), 'and');
            }
            if (!empty($param['mchid'])) {
                $search_mid = $param['mchid'];
            } else {
                $search_mid = $this->get_mer_mid();
            }
            if (!empty($param['search_date'])) {
                $search_time = explode(" - ", $param['search_date']);
                $start_date = strtotime($search_time[0]);
                $end_date = strtotime($search_time[1]);
            } else {
                if (empty($param['orderid'])) {
                    $start_date = strtotime(date('Y-m-d'));
                    $end_date = time();
                } else {
                    $start_date = 0;
                    $end_date = time();
                }
            }
            if (!empty($param['orderid'])) {
                $map['orderid'] = $param['orderid'];
            }
            if ($param['pay_fs'] === '0') {
                $map['pay_fs'] = 0;
            }else {
                if (!empty($param['pay_fs'])) {
                    $map['pay_fs'] = $param['pay_fs'];
                }
            }
            if (!empty($param['pay_statu'])) {
                $map['pay_statu'] = $param['pay_statu'];
            }
            $map['mid'] = array('in', $search_mid);
            $map['add_time'] = array(array('egt', $start_date), array('elt', $end_date), "and");
            $Paylog = new Paylog();
            $field = 'mid,orderid,yf_price,price,zk_price,service_charge,pay_fs,pay_statu,pay_cate,d_cashier,source,tk_price,add_time';
            if ($param['type'] == "search") {
                $list = $Paylog->with('machinfo')->where($map)
                    ->field($field)
                    ->limit(($param['pageNumber'] - 1) * $param['pageSize'], $param['pageSize'])->order('add_time desc')->select();
                $re_param["total"] = $Paylog->where($map)->count();
                //获取的记录
                $re_param["rows"] = $this->paylog_hand($list);
                return json($re_param);
            } elseif ($param['type'] == "out_excel") {
                $list = $Paylog->with('machinfo')->where($map)
                    ->field($field)
                    ->order('add_time desc')->select();
                foreach ($list as $k => $v) {
                    $lists[$k]['merchantname'] = $v['machinfo']['merchant'];
                    $lists[$k]['orderid'] = $v['orderid'];
                    $lists[$k]['yf_price'] = $v['yf_price'];
                    $lists[$k]['price'] = $v['price'];
                    $lists[$k]['zk_price'] = $v['zk_price'];
                    $lists[$k]['service_charge'] = $v['service_charge'];
                    $lists[$k]['pay_fs'] = getpayfs($v['pay_fs']);
                    $lists[$k]['pay_statu'] = getpaystatus($v['pay_statu']);
                    $lists[$k]['pay_cate'] = $v['pay_cate'];
                    $lists[$k]['d_cashier'] = $v['d_cashier'];
                    $lists[$k]['source'] = $v['source'];
                    $lists[$k]['tk_price'] = $v['tk_price'];
                    $lists[$k]['add_time'] = date('Y-m-d H:i:s', $v['add_time']);


                }
                $pub = new \app\common\controller\Pub();
                $header = ['门店名称', '订单号', '应收金额', '实收金额', '折扣金额','手续费', '支付方式', '交易状态', '交易类型', '收银员', '交易来源', '退款金额', '交易时间']; //表头,名称可自定义

                $excel = $pub->exportExcel($this->getmchname_mch($search_mid), $header, collection($lists)->toArray(),
                    'paylog_' . time() . rand(1000, 9999), "门店交易明细", "paylog", false);

                return "http://" . request()->server('HTTP_HOST') . "/" . $excel;
            }


        } else {
            $machlist = $this->getmchlist();
            $this->assign('machlist', json_decode($machlist, true));
            return $this->fetch();

        }

    }

    public function reportform()
    {
        if (request()->isAjax()) {
            $param = request()->post();
            $MchStatis = new MchStatis();
            if (!empty($param['search_date'])) {
                $date = explode(" - ", $param['search_date']);
                $start_time = strtotime($date[0]);
                $end_time = strtotime($date[1]);
            } else {
                $start_time = strtotime("last month") - 86400;
                $end_time = strtotime("-1 day");
            }
            if (!empty($param['mchid'])) {
                $mchid = ['in', $param['mchid']];
            } else {
                $mchid = ['in', $this->get_mer_mid()];
            }
            if ($param['type'] == "search") {


                $map['mid'] = $mchid;
                $map['statis_times'] = array(array('egt', $start_time), array('elt', $end_time), 'and');

                $list = $MchStatis
                    ->where($map)
                    ->field('statis_time,sum(wx_fee) as wx_fee,sum(wx_order) as wx_order, sum(ali_fee) as ali_fee,sum(ali_order) as ali_order,sum(cash_fee) as cash_fee,sum(cash_order) as cash_order,sum(wx_card_fee) as wx_card_fee,sum(wx_card_order) as wx_card_order,sum(dis_fee) as dis_fee,sum(ought_fee) as ought_fee,sum(real_fee-refund_fee) as real_fee,sum(avg_fee) as avg_fee,sum(refund_fee) as refund_fee,sum(refund_order) as refund_order ')
                    ->group('statis_time')->order('statis_time desc')->select();
//                $list = $map;
                return json($list);
            } elseif ($param['type'] == "out_excel") {
                $map['mid'] = ['in', $mchid];
                $map['statis_times'] = array(array('egt', $start_time), array('elt', $end_time), 'and');
                $list = $MchStatis
                    ->where($map)
                    ->field('statis_time,sum(wx_fee) as wx_fee,sum(wx_order) as wx_order, sum(ali_fee) as ali_fee,sum(ali_order) as ali_order,sum(cash_fee) as cash_fee,sum(cash_order) as cash_order,sum(wx_card_fee) as wx_card_fee,sum(wx_card_order) as wx_card_order,sum(dis_fee) as dis_fee,sum(ought_fee) as ought_fee,sum(real_fee-refund_fee) as real_fee,sum(avg_fee) as avg_fee,sum(refund_fee) as refund_fee,sum(refund_order) as refund_order ')
                    ->group('statis_time')->order('statis_time desc')->select();

                $pub = new \app\common\controller\Pub();
                $header = ['统计时间', '微信收款金额', '微信收款笔数', '支付宝收款金额', '支付宝收款笔数', '现金收款金额', '现金收款笔数', '微信会员卡支付金额', '微信会员卡支付笔数', '折扣金额', '应收金额', '实收金额', '客单价', '退款金额', '退款笔数']; //表头,名称可自定义

                $excel = $pub->exportExcel($this->getmchname_mch($mchid), $header, collection($list)->toArray(), 'reportform_' . time() . rand(1000, 9999), "门店交易报表", "reportform", false);;

                return "http://" . request()->server('HTTP_HOST') . "/" . $excel;
            }


        } else {
            $mchlist = $this->getmchlist();
            $this->assign('mchlist', json_decode($mchlist, true));
            return $this->fetch();
        }
    }

    private function paylog_hand($list)
    {
        foreach ($list as $k => $v) {
            $list[$k]['pay_statu'] = getpaystatus($v['pay_statu']);
            $list[$k]['add_time'] = date('Y-m-d H:i:s', $v['add_time']);
            $list[$k]['pay_fs'] = getpayfs($v['pay_fs']);
        }
        return $list;
    }

    //订单详情
    public function getpayinfo()
    {
        if (request()->isAjax()) {
            $map['orderid'] = request()->post('orderid');
            $Paylog = new Paylog();
            $info = $Paylog->where($map)->find();
            $info['machname'] = getmachname($info['mid']);
            $info['jtime'] = date('Y-m-d H:i:s', $info['add_time']);
            $info['pay_statu'] = getpaystatus($info['pay_statu']);
            $info['pay_fs'] = getpayfs($info['pay_fs']);
            $info['code'] = getcardcode_getid($info['cz_card_id']);
            $info['hx_card_list'] = getpaycard($info['hx_card']);
            return json($info);
        }
    }

    //退款列表
    public function gettklist()
    {
        if (request()->isAjax()) {
            $map['orderid'] = request()->post('orderid');
            $Tllog = new Tklog();
            $list = $Tllog->where($map)->select();
            foreach ($list as $k => $v) {
                $list[$k]['addtime'] = date('Y-m-d H:i:s', $v['addtime']);
            }
            return json($list);
        }
    }

    //退款详情
    public function gettkinfo()
    {

        if (request()->isAjax()) {
            $map['id'] = request()->post('id');
            $Tllog = new Tklog();
            $info = $Tllog->where($map)->find();
            $info['addtime'] = date('Y-m-d H:i:s', $info['addtime']);
            $info['machname'] = getmachname($info['mid']);
            $wxinfo = json_decode($info['tk_info'], true);
//            $info['transaction_id'] = $wxinfo['transaction_id'];
//            $info['refund_id'] = $wxinfo['refund_id'];
            return json($info);
        }

    }

    //商户名称
    public function requestname()
    {
        if (Request()->isAjax()) {
            $arr = request()->post();
            $Merchant = new Merchant();
            $map['merchant'] = ['like', '%' . trim($arr['merchant']) . '%'];
            $res = $Merchant->where($map)->field('merchant')->limit('5')->select();
            if (empty($res)) {
            } else {
                return json($res);
            }
        }
    }

    //交易方式统计
    public function paychannel()
    {
        if (request()->isAjax()) {
            $Paylog = new Paylog();

            if (request()->param('search_date') !== '') {
                $searchdate = explode(" - ", request()->param('search_date'));
                $starttime = strtotime($searchdate[0]);
                $endtime = strtotime($searchdate[1]);
            } else {
                $starttime = time() - 30 * 24 * 60 * 60;
                $endtime = time();
            }
            $map['add_time'] = [['EGT', $starttime], ['ELT', $endtime]];
//            $map['add_time'] = array('between', '' . $starttime . ',' . $endtime . '');
            if (request()->param('machname') !== '') {
                $map['mid'] = getmid(trim(request()->post('machname')));
            } else {
                $map['mid'] = array('in', $this->get_mer_mid());
            }

            $map['pay_statu'] = array(array('neq', 0), array('neq', 8), 'and');
            //微信支付
            $map1 = $map;
            $map1['pay_fs'] = 0;
//            $arrwx=$Paylog->where($map1)->field('sum(price) as wx_fee,count(id) as wx_order,sum(yf_price) as ought_fee')->find();
            $arr['wx_order'] = $Paylog->where($map1)->count();
            $arr['wx_fee'] = $Paylog->where($map1)->sum('price');
            $arr['wx_yf_fee'] = $Paylog->where($map1)->sum('yf_price');
            $arr['wx_refund_fee'] = $Paylog->where($map1)->sum('tk_price');
            $arr['wx_zk_fee'] = $Paylog->where($map1)->sum('zk_price');

            //微信刷卡支付
            $map2 = $map;
            $map2['pay_fs'] = 2;
            $arr['wx_barcode_order'] = $Paylog->where($map2)->count();
            $arr['wx_barcode_fee'] = $Paylog->where($map2)->sum('price');
            $arr['wx_barcode_yf_fee'] = $Paylog->where($map2)->sum('yf_price');
            $arr['wx_barcode_refund_fee'] = $Paylog->where($map2)->sum('tk_price');
            $arr['wx_barcode_zk_fee'] = $Paylog->where($map2)->sum('zk_price');

            //支付宝支付
            $map3 = $map;
            $map3['pay_fs'] = 1;
            $arr['ali_order'] = $Paylog->where($map3)->count();
            $arr['ali_fee'] = $Paylog->where($map3)->sum('price');
            $arr['ali_yf_fee'] = $Paylog->where($map3)->sum('yf_price');
            $arr['ali_refund_fee'] = $Paylog->where($map3)->sum('tk_price');
            $arr['ali_zk_fee'] = $Paylog->where($map3)->sum('zk_price');

            //支付宝刷卡支付
            $map4 = $map;
            $map4['pay_fs'] = 3;
            $arr['ali_barcode_order'] = $Paylog->where($map4)->count();
            $arr['ali_barcode_fee'] = $Paylog->where($map4)->sum('price');
            $arr['ali_barcode_yf_fee'] = $Paylog->where($map4)->sum('yf_price');
            $arr['ali_barcode_refund_fee'] = $Paylog->where($map4)->sum('tk_price');
            $arr['ali_barcode_zk_fee'] = $Paylog->where($map4)->sum('zk_price');

            //会员卡支付
            $map5 = $map;
            $map5['pay_fs'] = 8;
            $arr['wx_card_order'] = $Paylog->where($map5)->count();
            $arr['wx_card_fee'] = $Paylog->where($map5)->sum('price');
            $arr['wx_card_yf_fee'] = $Paylog->where($map5)->sum('yf_price');
            $arr['wx_card_refund_fee'] = $Paylog->where($map5)->sum('tk_price');
            $arr['wx_card_zk_fee'] = $Paylog->where($map5)->sum('zk_price');

            //现金支付
            $map6 = $map;
            $map6['pay_fs'] = 4;
            $arr['cash_order'] = $Paylog->where($map6)->count();
            $arr['cash_fee'] = $Paylog->where($map6)->sum('price');
            $arr['cash_yf_fee'] = $Paylog->where($map6)->sum('price');
//            $arr['cash_yf_fee']=$Paylog->where($map6)->sum('yf_price');
            $arr['cash_refund_fee'] = $Paylog->where($map6)->sum('tk_price');
            $arr['cash_zk_fee'] = $Paylog->where($map6)->sum('zk_price');

            //合计
            $arr['z_order'] = $arr['wx_order'] + $arr['ali_order'] + $arr['wx_barcode_order'] + $arr['ali_barcode_order'] + $arr['wx_card_order'] + $arr['cash_order'];
            $arr['z_fee'] = $arr['wx_fee'] + $arr['ali_fee'] + $arr['wx_barcode_fee'] + $arr['ali_barcode_fee'] + $arr['wx_card_fee'] + $arr['cash_fee'];
            $arr['z_yf_fee'] = $arr['wx_yf_fee'] + $arr['ali_fee'] + $arr['wx_barcode_yf_fee'] + $arr['ali_barcode_fee'] + $arr['wx_card_yf_fee'] + $arr['cash_yf_fee'];
            $arr['z_refund_fee'] = $arr['wx_refund_fee'] + $arr['ali_refund_fee'] + $arr['wx_barcode_refund_fee'] + $arr['ali_barcode_refund_fee'] + $arr['wx_card_refund_fee'] + $arr['cash_refund_fee'];
            $arr['z_zk_fee'] = $arr['wx_zk_fee'] + $arr['ali_zk_fee'] + $arr['wx_barcode_zk_fee'] + $arr['ali_barcode_zk_fee'] + $arr['wx_card_zk_fee'] + $arr['cash_zk_fee'];

            return json($arr);
        }
        //获取门店列表
        if ($this->get_mer_mid() !== '') {
            $mapa['id'] = array('in', $this->get_mer_mid());
        } else {
            $mapa['id'] = '';
        }
        $Merchant = new Merchant();
        $machlist = $Merchant->where($mapa)->select();
        $p_data['machname'] = "";
        $p_data['search_date'] = date('Y-m-d H:i:s', time() - 30 * 24 * 60 * 60) . " - " . date('Y-m-d H:i:s');
        $this->assign('p_data', $p_data);
        $this->assign('machlist', $machlist);
        return $this->fetch();
    }

}